package com.nowcoder.community.controller;

import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * @author tang
 * @create 2020-11-01
 */
@Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunityConstant {
   @Autowired
    private DiscussPostService discussPostService;
   @Autowired
   private UserService userService;
   @Autowired
    private HostHolder hostHolder;
   @Autowired
   private CommentService commentService;
   @Autowired
   private LikeService likeService;

   @PostMapping("/add")
    @ResponseBody
    public String addDisscussPost(String title,String content){
       User user = hostHolder.getUser();
       if (user==null){
           return CommunityUtil.getJSONString(403,"YOU HAVEN'T LOGIN");
       }
       DiscussPost discussPost = new DiscussPost();
       discussPost.setUserId(user.getId());
       discussPost.setTitle(title);
       discussPost.setContent(content);
       discussPost.setCreateTime(new Date());
       discussPostService.addDiscussPost(discussPost);
       System.out.println(discussPost);

       //
       return CommunityUtil.getJSONString(0,"POST SUCCESSFUL");
   }

   @GetMapping("detail/{discussPostId}")
    public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page){
       DiscussPost post = discussPostService.findDiscussPostById(discussPostId);
       model.addAttribute("post",post);
       User user = userService.findUserById(post.getUserId());
       model.addAttribute("user",user);
       //like
      long likeCountVo = likeService.findEntityLikeCount(ENTITY_TYPE_POST,post.getId());
      model.addAttribute("likeCount",likeCountVo);
      //int likeStatus
      int likeStatus = hostHolder.getUser()==null?0:likeService.findEntityLikeStatus(hostHolder.getUser().getId(),ENTITY_TYPE_POST,discussPostId);
      model.addAttribute("likeStatus",likeStatus);

       page.setLimit(5);
       page.setPath("/discuss/detail"+discussPostId);
       page.setRows(post.getCommentCount());

      // 评论: 给帖子的评论
      // 回复: 给评论的评论
      // 评论列表
      List<Comment> commentList = commentService.findCommentByEntity(
              ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());
      // 评论VO列表
      List<Map<String, Object>> commentVoList = new ArrayList<>();
      if (commentList != null) {
         for (Comment comment : commentList) {
            // 评论VO
            Map<String, Object> commentVo = new HashMap<>();
            // 评论
            commentVo.put("comment", comment);
            // 作者
            commentVo.put("user", userService.findUserById(comment.getUserId()));
            likeCountVo = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT,comment.getId());
            commentVo.put("likeCount",likeCountVo);
            //int likeStatus
            likeStatus = hostHolder.getUser()==null?0:likeService.findEntityLikeStatus(hostHolder.getUser().getId(),ENTITY_TYPE_COMMENT,comment.getId());
            commentVo.put("likeStatus",likeStatus);
            // 回复列表
            List<Comment> replyList = commentService.findCommentByEntity(
                    ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
            // 回复VO列表
            List<Map<String, Object>> replyVoList = new ArrayList<>();
            if (replyList != null) {
               for (Comment reply : replyList) {
                  Map<String, Object> replyVo = new HashMap<>();
                  // 回复
                  replyVo.put("reply", reply);
                  // 作者
                  replyVo.put("user", userService.findUserById(reply.getUserId()));
                  // 回复目标
                  User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getTargetId());
                  replyVo.put("target", target);

                  likeCountVo = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT,reply.getId());
                  replyVo.put("likeCount",likeCountVo);
                  //int likeStatus
                  likeStatus = hostHolder.getUser()==null?0:likeService.findEntityLikeStatus(hostHolder.getUser().getId(),ENTITY_TYPE_COMMENT,reply.getId());
                  replyVo.put("likeStatus",likeStatus);


                  replyVoList.add(replyVo);
               }
            }
            commentVo.put("replys", replyVoList);

            // 回复数量
            int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId());
            commentVo.put("replyCount", replyCount);

            commentVoList.add(commentVo);
         }
      }

      model.addAttribute("comments", commentVoList);

      return "/site/discuss-detail";
   }

}
